package structures;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class State implements Comparable<State> {
	public boolean isFinalState;
	public boolean isStartState;
	
	private String action;

	public boolean marked = false;
	public int index = 0;
	public int colour = 0;

	private Map<State, Set<String>> edges;

	public String toString(){
		return Integer.toString(index);
	}
	
	public State() {
		isFinalState = false;
		edges = new HashMap<State, Set<String>>();
	}

	public State[] getStatesArray() {
		HashSet<State> set = getStates();
		set.add(this);
		Object[] states = set.toArray();
		State[] result = new State[states.length];
		for (int i = 0; i != result.length; ++i) {
			result[i] = (State) states[i];
			result[i].marked = false;
		}

		return result;
	}

	public HashSet<State> getStates()// dfs
	{
		if (marked){
			return new HashSet<State>();
		}
		marked = true;
		HashSet<State> result = new HashSet<State>();
		for (Map.Entry<State, Set<String>> entry : edges.entrySet()) {
			result.add(entry.getKey());
			result.addAll(entry.getKey().getStates());
		}
		return result;
	}

	public void addEdge(State endState, String value) {
		if (!edges.containsKey(endState)) {
			edges.put(endState, new HashSet<String>());
		}
		edges.get(endState).add(value);
	}

	public boolean isFinalState() {
		return isFinalState;
	}

	public void setFinalState(boolean isFinalState) {
		this.isFinalState = isFinalState;
	}

	public Map<State, Set<String>> getEdges() {
		return edges;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	@Override
	public int compareTo(State o) {
		if (o.isStartState)
			return 1;
		if (isStartState)
			return -1;
		
		return 0;
	}
}
